package com.dindang.propertyfee.mvp.ui.adapter.custom;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dindang.propertyfee.R;
import com.dindang.propertyfee.mvp.model.entity.PropertyCharge;

import java.util.List;

/**
 * Created by cxw on & 2018/11/22
 */
public class FeeAdapter extends BaseExpandableListAdapter {

    private List<PropertyCharge.Total> list;
    private Context context;
    private OnFeeAdapterListener listener;

    public FeeAdapter(List<PropertyCharge.Total> list, Context context, OnFeeAdapterListener listener){
        this.list = list;
        this.context = context;
        this.listener = listener;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).costItems.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).costItems.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.propertyfee_item_property_total, null);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        PropertyCharge.Total total = (PropertyCharge.Total)getGroup(groupPosition);
        groupViewHolder.tvAddress.setText(total.address);
        groupViewHolder.tvTotalPrice.setText("￥" + total.total);
        groupViewHolder.tvNamePhone.setText(total.name + "/" + total.phone);

        if(total.isCheck) {
            groupViewHolder.aSwitch.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_single_select_press));
        }else{
            groupViewHolder.aSwitch.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_single_select_normal));
        }

        if(isExpanded){
            groupViewHolder.ivArrow.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_arrow_up));
            groupViewHolder.tvArrow.setText("收起");
        }else{
            groupViewHolder.ivArrow.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_arrow_down));
            groupViewHolder.tvArrow.setText("明细");
        }

        groupViewHolder.aSwitch.setOnClickListener(view -> listener.onClickGroup(groupPosition));

        if(groupPosition == 0){
            groupViewHolder.view_margin_top.setVisibility(View.GONE);
        }else{
            groupViewHolder.view_margin_top.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.propertyfee_item_property_cost, null);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        PropertyCharge.CostItem costItem = (PropertyCharge.CostItem) getChild(groupPosition, childPosition);
        childViewHolder.tvCostItem.setText(costItem.item);
        childViewHolder.tvCost.setText("￥" + costItem.cost);
        if(costItem.isDetailVisibility) {
            childViewHolder.view_detail.setVisibility(View.VISIBLE);
            childViewHolder.ivArrow.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_arrow_up2));
        }else{
            childViewHolder.view_detail.setVisibility(View.GONE);
            childViewHolder.ivArrow.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_arrow_down2));
        }
        childViewHolder.tvDosage.setText("用量:" + costItem.dosage);
        childViewHolder.tvNewRead.setText("本次读数:" + costItem.newRead);
        childViewHolder.tvOldRead.setText("上次读数:" + costItem.oldRead);
        childViewHolder.tvUnitPrice.setText("单价:" + costItem.unitPrice);
        childViewHolder.tvDataInterval.setText("费用期间:" + costItem.startData + "-" + costItem.endData);
        childViewHolder.clCostItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(costItem.isDetailVisibility){
                    costItem.isDetailVisibility = false;
                }else{
                    costItem.isDetailVisibility = true;
                }
                notifyDataSetChanged();
            }
        });

        if(costItem.isCheck) {
            childViewHolder.aSwitch.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_select_press));
        }else{
            childViewHolder.aSwitch.setImageDrawable(context.getDrawable(R.mipmap.propertyfee_ic_select_normal));
        }

        childViewHolder.aSwitch.setOnClickListener(view -> listener.onClickChild(groupPosition,childPosition));


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    static class GroupViewHolder {
        TextView tvAddress;
        TextView tvTotalPrice;
        TextView tvNamePhone;
        ImageView aSwitch;
        ImageView ivArrow;
        TextView tvArrow;

        View view_margin_top;

        public GroupViewHolder(View itemView) {
            tvAddress = itemView.findViewById(R.id.propertyfee_tv_address);
            tvTotalPrice = itemView.findViewById(R.id.propertyfee_tv_total_price);
            tvNamePhone = itemView.findViewById(R.id.propertyfee_tv_name_phone);
            aSwitch = itemView.findViewById(R.id.propertyfee_switchChecked);
            ivArrow = itemView.findViewById(R.id.propertyfee_iv_arrow);
            tvArrow = itemView.findViewById(R.id.propertyfee_tv_arrow);

            view_margin_top = itemView.findViewById(R.id.propertyfee_view_margin_top);
        }
    }

    static class ChildViewHolder {
        LinearLayout clCostItem;
        TextView tvCostItem;
        TextView tvCost;
        ImageView aSwitch;
        ConstraintLayout view_detail;
        ImageView ivArrow;

        TextView tvDosage;
        TextView tvNewRead;
        TextView tvOldRead;
        TextView tvUnitPrice;
        TextView tvDataInterval;

//        View viewTc;

        public ChildViewHolder(View itemView) {
            clCostItem = itemView.findViewById(R.id.propertyfee_cl_cost_item);
            tvCostItem = itemView.findViewById(R.id.propertyfee_tv_cost_item);
            tvCost = itemView.findViewById(R.id.propertyfee_tv_cost);
            aSwitch = itemView.findViewById(R.id.propertyfee_switchChecked);
            ivArrow = itemView.findViewById(R.id.propertyfee_iv_arrow);
            view_detail = itemView.findViewById(R.id.propertyfee_view_detail);

            tvDosage = itemView.findViewById(R.id.propertyfee_tv_dosage);
            tvNewRead = itemView.findViewById(R.id.propertyfee_tv_new_read);
            tvOldRead = itemView.findViewById(R.id.propertyfee_tv_old_read);
            tvUnitPrice = itemView.findViewById(R.id.propertyfee_tv_unit_price);
            tvDataInterval = itemView.findViewById(R.id.propertyfee_tv_data_interval);

//            viewTc = itemView.findViewById(R.id.propertyfee_view_tc);
        }
    }

    public interface OnFeeAdapterListener {
        void onClickChild(int groupPosition, int childPosition);
        void onClickGroup(int groupPosition);
    }
}
